#ifndef CRYPTO_H
#define CRYPTO_H

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

// 错误码定义
#define CRYPTO_SUCCESS 0
#define CRYPTO_ERROR_INVALID_PARAM -1
#define CRYPTO_ERROR_BUFFER_TOO_SMALL -2
#define CRYPTO_ERROR_INVALID_KEY -3
#define CRYPTO_ERROR_INVALID_IV -4
#define CRYPTO_ERROR_INVALID_SIGNATURE -5

// 常量定义
#define AES_BLOCK_SIZE 16
#define AES_KEY_SIZE_128 16
#define AES_KEY_SIZE_192 24
#define AES_KEY_SIZE_256 32
#define SHA256_DIGEST_SIZE 32
#define SHA512_DIGEST_SIZE 64
#define RSA_KEY_SIZE_2048 256
#define RSA_KEY_SIZE_4096 512

// 哈希算法
typedef struct {
    uint32_t state[8];
    uint32_t count[2];
    uint8_t buffer[64];
} sha256_ctx_t;

typedef struct {
    uint64_t state[8];
    uint64_t count[2];
    uint8_t buffer[128];
} sha512_ctx_t;

// AES 加密上下文
typedef struct {
    uint32_t round_keys[60];
    int rounds;
} aes_ctx_t;

// RSA 密钥对
typedef struct {
    uint8_t *n;  // 模数
    uint8_t *e;  // 公钥指数
    uint8_t *d;  // 私钥指数
    size_t n_len;
    size_t e_len;
    size_t d_len;
} rsa_keypair_t;

// 函数声明

// SHA256 哈希算法
int sha256_init(sha256_ctx_t *ctx);
int sha256_update(sha256_ctx_t *ctx, const uint8_t *data, size_t len);
int sha256_final(sha256_ctx_t *ctx, uint8_t *digest);
int sha256_hash(const uint8_t *data, size_t len, uint8_t *digest);
void sha256_transform(sha256_ctx_t *ctx, const uint8_t *data);

// SHA512 哈希算法
int sha512_init(sha512_ctx_t *ctx);
int sha512_update(sha512_ctx_t *ctx, const uint8_t *data, size_t len);
int sha512_final(sha512_ctx_t *ctx, uint8_t *digest);
int sha512_hash(const uint8_t *data, size_t len, uint8_t *digest);

// AES 加密算法
int aes_init(aes_ctx_t *ctx, const uint8_t *key, size_t key_len);
int aes_encrypt_ecb(aes_ctx_t *ctx, const uint8_t *plaintext, uint8_t *ciphertext);
int aes_decrypt_ecb(aes_ctx_t *ctx, const uint8_t *ciphertext, uint8_t *plaintext);
int aes_encrypt_cbc(aes_ctx_t *ctx, const uint8_t *plaintext, size_t len, 
                   const uint8_t *iv, uint8_t *ciphertext);
int aes_decrypt_cbc(aes_ctx_t *ctx, const uint8_t *ciphertext, size_t len,
                   const uint8_t *iv, uint8_t *plaintext);

// RSA 加密算法
int rsa_generate_keypair(rsa_keypair_t *keypair, size_t key_size);
int rsa_encrypt(const rsa_keypair_t *keypair, const uint8_t *plaintext, 
               size_t plaintext_len, uint8_t *ciphertext);
int rsa_decrypt(const rsa_keypair_t *keypair, const uint8_t *ciphertext,
               size_t ciphertext_len, uint8_t *plaintext);
int rsa_sign(const rsa_keypair_t *keypair, const uint8_t *data, size_t data_len,
            uint8_t *signature);
int rsa_verify(const rsa_keypair_t *keypair, const uint8_t *data, size_t data_len,
              const uint8_t *signature);

// 密钥派生函数
int pbkdf2_sha256(const uint8_t *password, size_t password_len,
                  const uint8_t *salt, size_t salt_len,
                  uint32_t iterations, uint8_t *key, size_t key_len);

// HMAC-SHA256
int hmac_sha256(const uint8_t *key, size_t key_len,
                const uint8_t *data, size_t data_len,
                uint8_t *mac);

// 随机数生成
int crypto_random_bytes(uint8_t *buffer, size_t len);

// 安全函数
void crypto_secure_zero(void *ptr, size_t len);
int crypto_secure_compare(const uint8_t *a, const uint8_t *b, size_t len);
int crypto_generate_iv(uint8_t *iv, size_t iv_len);
int crypto_generate_salt(uint8_t *salt, size_t salt_len);
int crypto_hash_sha256(const char *data, uint8_t *digest);

// 十六进制转换
void crypto_bytes_to_hex(const uint8_t *bytes, size_t len, char *hex);
int crypto_hex_to_bytes(const char *hex, uint8_t *bytes, size_t *len);

// 内存清理
void rsa_free_keypair(rsa_keypair_t *keypair);

#ifdef __cplusplus
}
#endif

#endif // CRYPTO_H 